<?php
//calculates the luminance difference between two hex colours
//source: http://www.splitbrain.org/blog/2008-09/18-calculating_color_contrast_with_php
function lumdiff($colour1, $colour2) {

    list($R1, $G1, $B1) = str_split(str_replace('#', '', $colour1), 2);
    list($R2, $G2, $B2) = str_split(str_replace('#', '', $colour2), 2);

    $L1 = 0.2126 * pow(hexdec($R1) / 255, 2.2) +
          0.7152 * pow(hexdec($G1) / 255, 2.2) +
          0.0722 * pow(hexdec($B1) / 255, 2.2);
 
    $L2 = 0.2126 * pow(hexdec($R2) / 255, 2.2) +
          0.7152 * pow(hexdec($G2) / 255, 2.2) +
          0.0722 * pow(hexdec($B2) / 255, 2.2);
 
    if($L1 > $L2) {
        return ($L1 + 0.05) / ($L2 + 0.05);
    } else {
        return ($L2 + 0.05) / ($L1 + 0.05);
    }
}
?>
